//
//  MLZViewController.m
//  MotherLover
//
//  Created by lanou on 15/12/31.
//  Copyright © 2015年 zj. All rights reserved.
//

#import "MLZViewController.h"
#import "MLZPlaylistcollecioncell.h"
#import "zJRequestManager.h"
#import "MLZPlayListmodel.h"
#import "MLZPlayListDeatlisController.h"
#import "MLZLogINViewController.h"
@interface MLZViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong)  UICollectionView * collection;
@property (nonatomic, strong) NSMutableArray *listArray;
@end

@implementation MLZViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

  
    
    self.view.backgroundColor =[UIColor whiteColor];
    
    
    [self addplaylistCollectionView];
    [self passingData];
    self.navigationController.navigationBar.hidden= YES;
    //[self add];
    
}
-(void)viewWillAppear:(BOOL)animated{

self.tabBarController.tabBar.hidden= NO;

}

-(void)add{


    UIScrollView *scro = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 375, 667)];
    scro.contentSize = CGSizeMake(KW*3, KH);

    scro.bounces= YES;
    
    [self.view addSubview:scro];

    


}


//解析数据
- (void)passingData{
   
    [zJRequestManager requestWithUrl:@"http://app.beva.com/box-packageItemData/p46/chbeva/kwerge--v2.0.json" parmDic:nil method:GET succeedWithData:^(NSData *data){
        
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
       
    NSArray *alllist = dic[@"data"];
    _listArray = [NSMutableArray array];
    for (NSDictionary *onedic in alllist) {
        MLZPlayListmodel *model = [[MLZPlayListmodel alloc]init];
        [model setValuesForKeysWithDictionary:onedic];
        [_listArray addObject:model];
    }
    [self.collection reloadData];
        
    } failedWithError:^(NSError *connectionError) {
    NSLog(@"%@",connectionError);
    }];
 



}
//布置界面
- (void)addplaylistCollectionView{

    UICollectionViewFlowLayout *layout= [[UICollectionViewFlowLayout alloc ]init];
    layout.minimumInteritemSpacing = COLeftandRight;
    layout.minimumLineSpacing = COLeftandRight;
    //分区内边距 上下左右的缩进距离
    layout.sectionInset= UIEdgeInsetsMake(COPadding, COPadding, COPadding, COPadding);
    //滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //    item大小
    layout.itemSize= CGSizeMake(COitems, COitems);
    //表头表尾
    //layout.headerReferenceSize= CGSizeMake(0, 150);
   // layout.footerReferenceSize= CGSizeMake(0, 150);
    _collection = [[UICollectionView alloc]initWithFrame:CGRectMake(COPadding, 64, KW, KH-64)  collectionViewLayout:layout];
    _collection.backgroundColor = [UIColor whiteColor];
    _collection.delegate =self;
    _collection.dataSource = self;
    [self.view addSubview:_collection];
    
    [_collection registerClass:[MLZPlaylistcollecioncell class] forCellWithReuseIdentifier:@"ZJCell"];
    
  //头像button
    UIButton *left = [UIButton buttonWithType:(UIButtonTypeCustom)];
//    [left setBackgroundImage:[UIImage imageNamed:@"left"] forState:(UIControlStateNormal)];
    left.frame = CGRectMake(20, 20, 40, 40);
    left.backgroundColor = [UIColor blackColor];
    [left.layer setCornerRadius:20];
    left.layer.masksToBounds= YES;
    
    
    [left addTarget:self action:@selector(leftAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:left];


}
#pragma mark -------点击出来左视图的
-(void)leftAction:(UIButton *)button{

    MLZLogINViewController *logIn = [[MLZLogINViewController alloc]init];
    [self.navigationController pushViewController:logIn animated:YES];


}
#pragma mark -----------collview需要的
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{



    return _listArray.count;
    


}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{



    MLZPlaylistcollecioncell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZJCell" forIndexPath:indexPath];

    cell.model = _listArray[indexPath.row];
    

    return cell;

}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    MLZPlayListDeatlisController *ListDeatlis = [[MLZPlayListDeatlisController alloc]init];
    MLZPlayListmodel*model =  _listArray[indexPath.row];
    ListDeatlis.zjid = model.zjId;
    [self.navigationController pushViewController:ListDeatlis animated:YES];
    
   


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
